1
직접적인 API 호출에서 LangChain 추상화로
AI010Lesson 5
00:00

원시 요청을 넘어서

대규모 언어 모델(대형언어모델, LLM)을 시작할 때 개발자들은 일반적으로 직접적인 API 호출(예: OpenAI 파이썬 라이브러리)을 사용하여 프롬프트를 보내고 완성된 결과를 받습니다. 기능적으로는 작동하지만, 애플리케이션이 확장될수록 이 접근 방식은 관리하기 어려워집니다.

무상태성의 문제

대규모 언어 모델은 본질적으로 무상태적. 매번 메시지를 보낼 때마다 모델은 당신이 누구인지, 그리고 이전에 무엇을 말했는지 '잊어버립니다'. 각 상호작용은 완전히 비어 있는 상태입니다. 대화를 유지하려면, 매번 전체 대화 기록을 수동으로 모델에 다시 전달해야 합니다.

LangChain의 해결책

LangChain은 ChatOpenAI모델 래퍼를 도입합니다. 단순한 래핑이 아니라, 모듈성의 기초가 됩니다. 모델 호출을 추상화함으로써, 나중에 모델을 교체하거나 메모리를 주입하고 템플릿을 사용할 수 있으며, 전체 코드베이스를 다시 작성하지 않아도 됩니다.

해적 시나리오
고객 이메일이 "해적 스타일"의 은유어로 작성되어 있다고 상상해보세요. 이를 공식적인 기업 답변으로 번역하려면 직접적인 API 호출은 지침을 하드코딩해야 합니다. 그러나 LangChain을 사용하면, "스타일"(해적 스타일 대비 공식 스타일)과 "내용"(이메일 본문)을 추상화를 통해 분리할 수 있습니다.
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
Question 1
Why do we say LLMs are "stateless"?
They do not have access to the internet.
They cannot generate the same response twice.
They do not inherently remember previous messages in a conversation.
They are only capable of processing text, not data states.
Challenge: Initialize ChatOpenAI
Solve the problem below.
You are building a creative writing assistant and need to initialize your first LangChain model.

Your task is to create a ChatOpenAI instance named my_llm with a temperature of 0.7 to allow for more creative (non-deterministic) responses.
Task
Write the Python code to import and initialize the model.
Solution:
from langchain_openai import ChatOpenAI
my_llm = ChatOpenAI(temperature=0.7)